<?

class OrdiniLista
{
	var $id_negozio;
	var $lista;
	var $dettagli;
	var $db;
	
	function OrdiniLista($id_negozio, &$db)
	{
		$this->id_negozio = $id_negozio;
		$this->db = &$db;		
	}
	
	function load_ultimi($limit=10, $nascondi_cancellati=true)
	{
		if($nascondi_cancellati) $qpiece = " AND ordini.status!=\"cancellato\" ";
		$query = "SELECT ordini.*, SUM(ordini_righe.imponibile) as totale,SUM(ordini_righe.quantita) as colli, GROUP_CONCAT(DISTINCT stati_ordini.id_stato ORDER BY data_mod DESC SEPARATOR ' ') as stati_ordine
					FROM moka_ecomm_$this->id_negozio.ordini 
					LEFT JOIN moka_ecomm_$this->id_negozio.ordini_righe ON ordini_righe.id_ordine = ordini.id
					LEFT JOIN moka_ecomm_2.stati_ordini ON stati_ordini.id_ordine = ordini.id 
					WHERE 1=1 $qpiece
					GROUP BY ordini.id ORDER BY data_ins DESC LIMIT $limit";
		$result = $this->db->query($query);
		if(mysql_error())echo $query." -> ".mysql_error();
		while($row = mysql_fetch_object($result))
		{
			foreach($row as $nom=>$val) $this->lista[$nom][$row->id] = $val;
			$tmp = explode(" ",$row->stati_ordine);
			$this->lista[stato][$row->id] = $tmp[0];
			$lista_id .= "$row->id," ;
			$this->lista[totale][$row->id_ordine] += $row->totale;
			$this->lista[colli][$row->id_ordine] += $row->colli;
		}
		
		
	}
	
	function Delete($id_ordine)
	{

		$query = "UPDATE moka_ecomm_$this->id_negozio.ordini SET status=\"cancellato\" WHERE id='".$id_ordine."'";
		$rd = $this->db->query($query);
		if(mysql_error())echo $query." -- ".mysql_error();
	}
	
}
?>